var ClubDataOverviewTab = Item.extend({
    _ccsFileName: res.ClubDataOverviewTab1_json,
    _clubId: null,
    _tableViewList: [],

    _selectPos: 0,
    _selectedMapIdStr: '',
    _selectGroupPlayers: null,
    _selectStartData: null,   //选择的查询起始时间
    _selectEndData: null,    //选择的查询结束时间
    _scoreArea: '',

    _startYear: null,  //开始年
    _startMonth: null, //开始月
    _startDete: null,  //开始日

    _endYear: null,  //结束年
    _endMonth: null, //结束月
    _endDete: null,  //结束日

    _currentSelect: 0,
    _currentselectYear: null,    //日期选择的 年
    _currentselectMonth: null,   //日期选择的 月
    _currentselectday: null,     //日期选择的 日

    initItem: function (clubId) {
        this._clubId = clubId;
        this._refreshGroupBtnView();
        this._nodeList['btn_allWanfa.label'].setString('全部玩法');
        this._initTableView();

        var today = new Date();
        this._startYear = this._endYear = today.getFullYear();
        this._startMonth = this._endMonth = today.getMonth() + 1;
        this._startDete = this._endDete = today.getDate();


        this._currentselectYear = this._endYear;
        this._currentselectMonth = this._endMonth;
        this._currentselectday = this._endDete;

        //设置按钮名称 用来区分点击的事那个时间按钮
        this._nodeList['dateStartNode.btn_date'].setName('startDate');
        this._nodeList['dateEndNode.btn_date'].setName('endDate');

        this._refreshDateView(this._nodeList['dateStartNode.dateLabel'], this._startYear, this._startMonth, this._startDete);
        this._refreshDateView(this._nodeList['dateEndNode.dateLabel'], this._endYear, this._endMonth, this._endDete);

        this._scoreArea = '0,999999';
        this._selectGroupPlayers = null;

    },

    _initClickListeners: function () {
        this._clickListeners = {
            'seeGroup': '_clickSeeGroup',
            'btn_allWanfa': '_clickWanFaFilter',
            'btn_edit': '_clickEdit',
            'dateStartNode.btn_date': '_clickDate',
            'dateEndNode.btn_date': '_clickDate',
            'btn_downday': '_clickDay',
            'btn_upday': '_clickDay'
        };
    },

    _initEventListeners: function () {
        this._eventListeners = {};
        this._eventListeners[ClubGroupSelectEvent] = '_selectGroupHandler';
        this._eventListeners[ClubUserEditOverEvent] = '_clubEditHanlder';
        this._eventListeners[ClubEvent.FloorAndWanfaFilterEvent] = '_clubWafnaHanlder';
        this._eventListeners[ClubRecordCode.DataOverview] = '_dataOverviewHanlder';
        this._eventListeners[ClubRecordCode.CardNum] = '_refreshCardNumHanlder';
        // this._eventListeners[ClubMsgCode.AllMemberInfo] = '_allMemberInfoHandler';
        // this._eventListeners[ClubMsgCode.QueryClubGroup] = '_queryClubGroupHandler';
    },

    /**
     * 初始化TableView
     */
    _initTableView: function () {
        var nodeContent = this._nodeList['nodeContent'];
        var tableViewSize = nodeContent.getContentSize();
        this._tableView = new cc.TableView(this, cc.size(tableViewSize.width, tableViewSize.height));
        this._tableView.setDelegate(this);
        this._tableView.setDirection(cc.SCROLLVIEW_DIRECTION_VERTICAL);
        this._tableView.setVerticalFillOrder(cc.TABLEVIEW_FILL_TOPDOWN);
        this._tableView.setPosition(nodeContent.getPosition());
        nodeContent.getParent().addChild(this._tableView);
    },

    /**
     * 刷新界面显示
     */
    refreshView: function () {
        // this._nodeList['card_num'].setString(gameData.numOfCards[1] + '张');
        this._nodeList['card_num'].setString('******张');
        this._changeFilter();
    },

    _changeFilter: function () {
        clubMsgManager.requestDataOverview(this._clubId,
            this._selectPos,
            this._selectGroupPlayers,
            this._nodeList['dateEndNode.dateLabel'].getString().replace(/\//g, '-'),
            this._nodeList['dateEndNode.dateLabel'].getString().replace(/\//g, '-'),
            this._selectedMapIdStr,
            this._scoreArea
        );

        clubMsgManager.requestClubOwnerFangKa(this._clubId);
    },

    _clickSeeGroup: function () {
        showWindow('ClubGroupWindow', GroupWindowType_See, this._clubId);
    },

    _clickWanFaFilter: function () {
        showWindow('ClubFloorsWanfaFilterWindow', this._clubId);
    },

    _clickEdit: function () {
        showWindow('ClubUserEditScoreWindow');
    },

    _clubWafnaHanlder: function (data) {
        if (!data) return;
        this._selectPos = null;
        this._selectedMapIdStr = null;
        if (data['pos']) {
            this._selectPos = data['pos'];
        } else if (data['mapId']) {
            this._selectedMapIdStr = data['mapId'];
        }
        this._nodeList['btn_allWanfa.label'].setString(data['name']);
        this._changeFilter();
    },

    _clickDate: function (target) {
        var today = new Date();
        var curyear = today.getFullYear();
        var curmonth = today.getMonth() + 1;
        var curday = today.getDate();

        var dateLabel = 'dateStartNode.dateLabel';
        if (target.getName() == 'endDate') {
            dateLabel = 'dateEndNode.dateLabel';
            var _clubPowerDate = new ClubPowerDate(curyear, curmonth, curday, this._powerDateHandler.bind(this, dateLabel));
            window.curScene.addChild(_clubPowerDate, 99);
        } else {
            var _clubPowerDate = new ClubPowerDate(curyear, curmonth, curday, this._powerDateHandler.bind(this, dateLabel), null, null, 1);
            window.curScene.addChild(_clubPowerDate, 99);
        }
    },

    /**
     * 当前选中分组
     * @private
     */
    _selectGroupHandler: function (data) {
        if (data['type'] && data['type'] == ClubGroupBtn_All) {
            this._selectGroupPlayers = null;
        } else {
            this._selectGroupPlayers = data['uids'].join(',');
        }
        this._refreshGroupBtnView(data['name']);
        this._changeFilter();
    },

    _clubEditHanlder: function (data) {
        var leftValue = data['leftValue'] == '0' ? '0' : data['leftValue'];
        var rightValue = data['rightValue'] == '0' ? '0' : data['rightValue'];
        if (rightValue == '0') {
            rightValue = '99999'
        }
        this._nodeList['dangweiLabel'].setString('分数:' + leftValue + '-' + rightValue);
        if (leftValue == '0' && rightValue == '0') {
            this._scoreArea = '0,99999';
            this._nodeList['dangweiLabel'].setString('分数:不限');
        } else {

            this._scoreArea = leftValue + ',' + rightValue
        }
        this._changeFilter();
    },

    /**
     * 刷新分组按钮 文字显示
     * @private
     */
    _refreshGroupBtnView: function (str) {
        if (str == null) {
            str = '全部分组';
        }
        this._nodeList['seeGroup.label'].setString(str);
    },

    _dataOverviewHanlder: function (data) {
        //设置总开放数和总消耗
        var total_fangka_num = 0;
        var total_xiaohao_num = 0;
        for (var i = 0; i < data.length; i++){
            total_fangka_num += (Number(data[i]['roomCount']) || 0);
            total_xiaohao_num += (Number(data[i]['userCard']) || 0);
        }

        this._nodeList['total_fangka_num'].setString(total_fangka_num + '张');
        this._nodeList['total_fangka_used_num'].setString(total_xiaohao_num + '张');

        this._tableViewList = data.reverse();
        this._tableView.reloadData();
    },

    _refreshCardNumHanlder: function (cardNum) {
        this._nodeList['card_num'].setString(cardNum + '张');
    },

    tableCellAtIndex: function (table, idx) {
        var cell = table.dequeueCell();
        var node = null;
        if (cell == null) {
            cell = new cc.TableViewCell();
            node = addItem('ClubDataOverviewItem', cell, this._tableViewList[idx]);
            node.setName("itemCell");
        } else {
            node = cell.getChildByName("itemCell");
            node.setData(this._tableViewList[idx]);
        }
        return cell;
    },
    tableCellTouched: function (table, cell) {
    },
    tableCellSizeForIndex: function (table, idx) {
        return cc.size(1016, 64);
    },
    numberOfCellsInTableView: function (table) {
        return this._tableViewList.length;
    },
    /**
     * 时间选中回调结果
     * @private
     */
    _powerDateHandler: function (dateLabel, year, month, day) {
        this._currentSelect = 0;
        this._currentselectYear = year;
        this._currentselectMonth = month;
        this._currentselectday = day;
        this.refrendate(dateLabel, year, month, day);
    },
    refrendate:function (dateLabel, year, month, day) {
        if (dateLabel == 'dateStartNode.dateLabel') {
            this._startYear = year;
            this._startMonth = month;
            this._startDete = day;
        } else {
            this._endYear = year;
            this._endMonth = month;
            this._endDete = day;
        }
        this._refreshDateView(this._nodeList[dateLabel], year, month, day);
        this.refreshView();
    },

    /**
     * 刷新时间显示
     * @private
     */
    _refreshDateView: function (label, year, month, day) {
        if (month < 10) {
            month = '0' + month;
        }
        if (day < 10) {
            day = '0' + day;
        }
        label.setString(year + '/' + month + '/' + day);
    },
    _clickDay:function (target) {
        var btnName = target.getName();
        if (btnName == 'btn_upday') {
            this._currentSelect ++;
        } else {
            this._currentSelect --;
        }
        var selectday = new Date(this._currentselectYear+'/'+this._currentselectMonth+'/'+this._currentselectday+' 00:00:00');
        var jiange = selectday.getTime() - 86400000* this._currentSelect;
        var newdate =  new Date(jiange);
        var maxdate =  new Date();
        var mixdate =  new Date(selectday.getTime() - 86400000* 15);
        if(newdate.getTime() > maxdate.getTime() || newdate.getTime() < mixdate){
            if (btnName == 'btn_upday') {
                this._currentSelect --;
            } else {
                this._currentSelect ++;
            }
            return ;
        }
        var newyear = newdate.getFullYear();
        var newmonth  = newdate.getMonth()+1;
        var newday  = newdate.getDate();
        this.refrendate('dateEndNode.dateLabel',newyear,newmonth,newday);
    },

});